package com.lancoo.klgcourseware.utils;

import android.content.Context;
import androidx.core.content.ContextCompat;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.WordPracticeModel;
import com.stkouyu.entity.EvaluateWord;
import com.stkouyu.entity.LgEvaluateObj;

import java.util.List;

public class KlgTrainUtils {

    public static SpannableString getEvaluationResultSentence(Context context, String rawContent, WordPracticeModel wordPracticeModel, LgEvaluateObj skegnResult){
        SpannableString spannableString = new SpannableString(rawContent);

        List<EvaluateWord> evaluateWordList = skegnResult.getResult().getWords();

        int wordPosition = 0;

        int pIndex = 0;
        while (pIndex<rawContent.length()
                && wordPosition<evaluateWordList.size()){
            EvaluateWord evaluateWord = evaluateWordList.get(wordPosition);
            if ( (rawContent.charAt(pIndex)) != ' '
                    &&(rawContent.charAt(pIndex)) == evaluateWord.getWord().charAt(0)){

                Log.e("John","单词开始："+pIndex +":"+evaluateWord.getWord()+","+evaluateWord.getScores().getOverall() );
                if (evaluateWord.getScores().getOverall() < 85){
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.klg_color_error));
                    spannableString.setSpan(foregroundColorSpan,
                            pIndex, pIndex+evaluateWord.getWord().length(),
                            Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                pIndex += evaluateWord.getWord().length();
                wordPosition++;
            }else pIndex++;
        }

        return spannableString;
    }

    public static SpannableString getRawSentence(Context context, WordPracticeModel wordPracticeModel){
        StringBuilder sentenceStringBuffer=new StringBuilder();

//        Log.e("John","rawSentence:"+wordPracticeModel.getSentence());
        String sentence = wordPracticeModel.getSentence()
                .replaceAll("<strong>","")
                .replaceAll("</strong>","")
                .replaceAll("</STRONG>","")
                .replaceAll("<STRONG>","")
                .replaceAll("<br>","")
                .replaceAll("<BR>","");
        sentence= Html.fromHtml(sentence).toString().trim().replaceAll("&\\*","&@&@&@&@&@&@&@&@&@&@");
//        wordPracticeModel.setSentence(sentence);
        String[] split = sentence.split("&@&@&@&@&@&@&@&@&@&@");
        int[] startIndexs = new int[wordPracticeModel.getHideWordList().size()];
        int[] endIndexs = new int[wordPracticeModel.getHideWordList().size()];
        int index=0;
        for (int i = 0; i < split.length; i++) {
            sentenceStringBuffer.append(split[i]);
            if(i<wordPracticeModel.getHideWordList().size()){
                String word = wordPracticeModel.getHideWordList().get(i).getWord();
                sentenceStringBuffer.append(word);
                index+=split[i].length();
                startIndexs[i] = index;
                endIndexs[i] = index+word.length();
                index+=word.length();
            }
        }
        SpannableString spannableString = new SpannableString(sentenceStringBuffer.toString());

        for (int i=0;i<startIndexs.length;i++){
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.klg_color_error));
            spannableString.setSpan(foregroundColorSpan, startIndexs[i], endIndexs[i], Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        }


        return spannableString;
    }

    public static int getAudioDBLevel(int db){
        return 10000*db/6;
    }
}
